package utilities

import (
	"mi/compiler/ast"
	"mi/compiler/scanner"
	"reflect"
)

func GetTextOfNode(node ast.Node, includeTrivia bool /*false*/ ) string {
	return GetSourceTextOfNodeFromSourceFile(ast.GetSourceFileOfNode(node), node, includeTrivia)
}

func GetSourceTextOfNodeFromSourceFile(sourceFile *ast.SourceFile, node ast.Node, includeTrivia bool) string {
	if ast.NodeIsMissing(node) {
		return ""
	}

	var text = sourceFile.Text
	var start int
	if includeTrivia {
		start = node.Pos()
	} else {
		start = scanner.SkipTrivia(text, node.Pos(), false, false)
	}
	return string(text[start:node.End()])
}

func GetTextOfNodeFromSourceText(sourceText []byte, node ast.Node) string {
	if ast.NodeIsMissing(node) {
		return ""
	}

	return string(sourceText[scanner.SkipTrivia(sourceText,node.Pos(), false, false):node.End()])
}

func isNull(i interface{}) bool {
	ret := i == nil

	if !ret {
		vi := reflect.ValueOf(i)
		kind := reflect.ValueOf(i).Kind()
		if kind == reflect.Slice ||
			kind == reflect.Map ||
			kind == reflect.Chan ||
			kind == reflect.Interface ||
			kind == reflect.Func ||
			kind == reflect.Ptr {
			return vi.IsNil()
		}
	}

	return ret
}

